package pro.spring.service;

import java.io.InputStream;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.baidu.inf.iis.bcs.BaiduBCS;
import com.baidu.inf.iis.bcs.auth.BCSCredentials;
import com.baidu.inf.iis.bcs.model.BCSClientException;
import com.baidu.inf.iis.bcs.model.BCSServiceException;
import com.baidu.inf.iis.bcs.model.DownloadObject;
import com.baidu.inf.iis.bcs.model.ObjectListing;
import com.baidu.inf.iis.bcs.model.ObjectMetadata;
import com.baidu.inf.iis.bcs.model.ObjectSummary;
import com.baidu.inf.iis.bcs.request.ListObjectRequest;
import com.baidu.inf.iis.bcs.response.BaiduBCSResponse;

import pro.spring.inter.InformationMapper;
import pro.spring.model.Information;
import pro.spring.vo.PaginationVo;

@Service
public class YunService {
	@Autowired
	private InformationMapper informationMapper;
	private static final Log log = LogFactory.getLog(YunService.class);
	static String host ="bcs.duapp.com";
	static String accessKey="9dc88ec38a7e34afbbc9125ebcbabe89";
	static String  secretKey="65007dbec4244ce579cffda5a52315de";
	static String bucket="dxbook";
	//static String object="/Backbonejs入门教程.pdf";
	//static File file = new File("E://EclipseWorkspace////pro04//wkxx//Backbonejs入门教程.pdf");
	BCSCredentials credentials = new BCSCredentials(accessKey,secretKey);
	BaiduBCS baiduBCS = new BaiduBCS(credentials,host);
	
	//上传	
	public void upload(CommonsMultipartFile file,Information information)throws Exception{
		baiduBCS.setDefaultEncoding("UTF-8");
		long fileSize =file.getSize();
		System.out.println(fileSize);
		String fileName=new Date().getTime()+file.getOriginalFilename();
		System.out.println(fileName);
		//String fileName=file.getOriginalFilename();
		//fileName含有后缀名
		//String fileType = fileName.substring(fileName.lastIndexOf("."));
		InputStream upFile = file.getInputStream();
		ObjectMetadata objMetadata = new ObjectMetadata();
		objMetadata.setContentLength(fileSize);
		//BaiduBCSResponse<ObjectMetadata> response =
		baiduBCS.putObject(bucket,"/"+fileName,upFile,objMetadata);
//		PutObjectRequest request = new PutObjectRequest(bucket,"/"+fileName,upFile,objMetadata);
//		BaiduBCSResponse<ObjectMetadata> response =baiduBCS.putObject(request);
		//ObjectMetadata objectMetadata = response.getResult();
//数据库操作
		information.setInTitle(file.getOriginalFilename());
		information.setInLocalhost(fileName);
		information.setInBrowse(0);
		//3为文件
		information.setInType(3);
		informationMapper.insert(information);
		
		//return 1;
	}
	//下载
	public InputStream download(Integer inId){
		baiduBCS.setDefaultEncoding("UTF-8");
		Information information=informationMapper.selectByPrimaryKey(inId);
		information.setInBrowse(information.getInBrowse()+1);
		String name = "/"+information.getInLocalhost();
		BaiduBCSResponse<DownloadObject> dob = null;
		try{
			dob=baiduBCS.getObject(bucket,name);
			DownloadObject obj = dob.getResult();
			InputStream in = obj.getContent();
			return in;
			//return obj;
		}catch(BCSServiceException e){ 
			log.warn("Bcs return:" + e.getBcsErrorCode() + ", " + e.getBcsErrorMessage() + ", RequestId=" + e.getRequestId());
			e.printStackTrace();
			return null;
		}catch(BCSClientException e){
			e.printStackTrace();
			return null;
		}
	}
	//得到文件大小
	public long getSize(Integer inId){
		Information information=informationMapper.selectByPrimaryKey(inId);
		String object="/"+information.getInLocalhost();
		BaiduBCSResponse<ObjectMetadata> objM = baiduBCS.getObjectMetadata(bucket, object);
		return objM.getResult().getContentLength();
	}
	//得到文件信息
	public Information getRealInfo(Integer inId){
		Information information = informationMapper.selectByPrimaryKey(inId);
		return information;
	}
	//文件大小转换
		public static String changeSize(long size){
			//KB 1024,MB 1048576 	
			float changeSize=0;
			if(size>=1048576){
				changeSize=size/1048576;
				return changeSize+"MB";
			}else if(size>=1024){
				changeSize = size/1024;
				return  changeSize+"KB";
			}else if(size<1024&&size>0){
				changeSize = size;
				return changeSize+"B";
			}else{
				return "数据异常";
			}
		}
		//查询
		public boolean found(String name){
			baiduBCS.setDefaultEncoding("UTF-8");
			try{
				boolean type;
				type=baiduBCS.doesObjectExist(bucket,"/"+name);
				return type;
			}catch(BCSServiceException e){
				log.warn("Bcs return:" + e.getBcsErrorCode() + ", " + e.getBcsErrorMessage() + ", RequestId=" + e.getRequestId());
				e.printStackTrace();
				return false;
			}catch(BCSClientException e){
				e.printStackTrace();
				return false;
			}
		}
		public PaginationVo<Information> getPaginationInfo(int pagenum,int pagesize){
			//int pagenum是当前的页码,int pagesize是每页显示的数据数量
			PaginationVo<Information> pageVo=new PaginationVo<Information>();
			//一定要在setPageCount()方法之后执行setPageNum
			pageVo.setCount(informationMapper.getCountByType(300));
			pageVo.setRows(pagesize);
			pageVo.setPageNum(pagenum);
			pageVo.setList(informationMapper.selectListPageByType(300,pageVo.getOffset(), pageVo.getRows()));
			return pageVo;
		}
		//百度云列表
//		public void list(){
//			baiduBCS.setDefaultEncoding("UTF-8");
//			try{
//				ListObjectRequest listObjectRequest =  new ListObjectRequest(bucket);
//				listObjectRequest.setStart(0);
//				listObjectRequest.setLimit(10);
//				listObjectRequest.setPrefix("//");//根目录
//				BaiduBCSResponse<ObjectListing> response = baiduBCS.listObject(listObjectRequest);
//				log.info("we get [" + response.getResult().getObjectSummaries().size() + "] object record.");
//				for (ObjectSummary os : response.getResult().getObjectSummaries()) {
//					log.info(os.toString());
//					System.out.println(os.getName());
//					System.out.println((changeSize(os.getSize())));
//					System.out.println(os.getLastModifiedTime());
//				}
//				
//				
//			}catch(BCSServiceException e){
//				log.warn("Bcs return:" + e.getBcsErrorCode() + ", " + e.getBcsErrorMessage() + ", RequestId=" + e.getRequestId());
//				e.printStackTrace();
//			}catch(BCSClientException e){
//				e.printStackTrace();
//			}
//			
//		}

}
